import { getSbData, supabase } from "../../utils/supabase";

export default {
  data() {
    return {
      agreeCheck: [],
      isNext: false,
      form: {
        mobile: "",
        vcode: "",
      },
      sending: false,
      validating: false,
    };
  },
  computed: {
    inMp() {
      return uni.$h.inMp;
    },
    isAgree() {
      return !!this.agreeCheck.length;
    },
    vcodeFilled() {
      return this.form.vcode.length == 4;
    },
  },
  watch: {
    vcodeFilled(val) {
      if (val) this.onValidate();
    },
  },
  methods: {
    async getPhoneNumber(e) {
      try {
        this.validating = true;
        const res = await supabase.auth.wechatBindPhone({
          code: e.detail.code,
        });
        const data = getSbData(res);
        console.log(data);
        // await this.$store.dispatch("getUserInfo");
        this.onDone();
      } catch (error) {
        console.log(error);
      }
      this.validating = false;
    },
    async onDone() {
      this.$toast("已登录", 1);
      await this.$sleep(300);
      if (this.loginTo) {
        this.$redirectTo(this.loginTo);
      } else {
        this.$navBack();
      }
    },
    goAgree(name = "agreement") {
      const title = name == "privacy" ? "隐私政策" : "用户协议";
      this.$navTo(`https://hqs.link/www/ice/${name}.html`, {
        title,
      });
    },
    setAgree() {
      this.agreeCheck = this.isAgree ? [] : ["1"];
    },
  },
};
